﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Framework.MVC;
using FrontCenter.Models;
using Framework.Common.Entry;

namespace FrontCenter.Controllers
{
    public class AnswerController : AsyncControllerBase
    {
        private const string m_user_resource = "u000";

        #region NewView

        public void NewViewAsync(string code) {
            HttpCookie user = Request.Cookies[m_user_resource];
            string current = user == null || string.IsNullOrWhiteSpace(user.Value) ? code : user.Value;
            var model = new ContainsAnswerModel
            {
                IdentityCode = current,
                Source = Request.UserHostAddress
            };
            ViewData["code"] = current;
            InitModel(model);
        }

        public ActionResult NewViewCompleted(string error) {
            ViewData["error"] = error;
            return View();
        }

        #endregion NewView

        #region New

        public void NewAsync(string code,KeyValueEntry<string,string>[] answers)
        {
            if (answers != null && answers.Length == 3)
            {
                for (int i = 0, count = 3; i < count; i++)
                {
                    var model = new NewAnswerModel
                    {
                        IdentityCode = code,
                        Answer =answers[i].value,
                        QuestionCode = answers[i].key,
                        Name = "result"+i,
                        Source = Request.UserHostAddress
                    };

                    InitModel(model);
                }
            }
        }

        public ActionResult NewCompleted(bool result0, bool result1, bool result2)
        {
            return Json(result1 || result2 || result0);
        }

        #endregion New

        #region Validate

        public void ValidateAsync(string code, string question, string answer)
        {
            var model = new ValidateAnswerModel
            {
                IdentityCode = code,
                Source = Request.UserHostAddress,
                QuestionCode = question,
                Answer = answer
            };
            InitModel(model);
        }

        public ActionResult ValidateCompleted(string code,string error)
        {
            return Json(error ?? code);
        }

        #endregion Validate
    }
}
